<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\StoreArticlePostRequest;
use App\Http\Controllers\Controller;
use DB;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = '文章列表';
         $data = DB::table('article')
          ->where('title','like','%'.$request ->input('keywords').'%')
          ->paginate($request ->input('num',10));   
          foreach ($data as $k =>$v)
          {
            $data[$k] -> create_at =date('Y年m月d日',$v -> create_at);
            $data[$k] -> update_at =date('Y年m月d日',$v -> update_at);
          }
         
        return view('admin.article.index',['title' =>$title,'data'=>$data,'request'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $title = '文章添加';
        // public function 

        return view('admin.article.add',['title'=>$title]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreArticlePostRequest $request)
    {
        // dd($request ->all());
       
        //验证

        //获取需要信息
        $data = $request ->except('_token');
        //处理img图片
        if($request -> hasFile('img'))
        {
            if($request -> file('img') -> isValid())
            {
                $suffix = $request -> file('img') -> getClientOriginalExtension();
                //生成随机文件名
                $fileName = time() . mt_rand(100000,999999).'.'.$suffix;
                //移动文件
                $t = date('Ymd').'/';
                $request -> file('img') -> move('./uploads/'.$t,$fileName);
                //处理img字段
                $data['img'] = '/uploads/'.$t.$fileName;
            }
        }
        $data['create_at'] = time();
        $data['update_at'] = time();
        $res = DB::table('article') -> insert($data);
        if($res)
        {
            return redirect('admin/article') ->with(['info' =>'添加成功']);
        }else
        {
            return back() ->with(['info' =>'添加失败']);
        }



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $title = '文章编辑';
        $data = DB::table('article') -> where('id',$id)->first();
        return view('admin.article.edit',['title'=>$title,'data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($request ->all());
        
        $data = $request ->except(['_token','_method','oldImg']);
         //处理img图片
        if($request -> hasFile('img'))
        {
            if($request -> file('img') -> isValid())
            {
                $suffix = $request -> file('img') -> getClientOriginalExtension();
                //生成随机文件名
                $fileName = time() . mt_rand(100000,999999).'.'.$suffix;
                //移动文件
                $t = date('Ymd').'/';
                $request -> file('img') -> move('./uploads/'.$t,$fileName);
                //处理img字段
                $data['img'] = '/uploads/'.$t.$fileName;

                //Huoqu老图片
                $oldImg = $request ->input('oldImg');
                if(file_exists('.'.$oldImg))
                {
                    unlink('.'.$oldImg);
                    
                }
                
            }
        }
        $data['update_at'] = time();
        
        $res = DB::table('article')->where('id',$id)->update($data);
         if($res)
        {
            return redirect('admin/article') ->with(['info' =>'更新成功']);
        }else
        {
            return back() ->with(['info' =>'更新失败']);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $oldImg =DB::table('article')->where('id',$id)->first()->img;
        $res = DB::table('article') ->where('id',$id) ->delete();

        if($res)
        {
            if(file_exists('.'.$oldImg))
            {
                unlink('.'.$oldImg);
                
            }
            return redirect('admin/article') ->with(['info' =>'删除成功']);

        }else
        {
            return back() ->with(['info' =>'删除失败']);
        }
    }
}
